<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="流程/区域" prop="processRegion">
        <el-input
          v-model="queryParams.processRegion"
          placeholder="请输入流程/区域"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有限空间名称" prop="confinedSpaceName">
        <el-input
          v-model="queryParams.confinedSpaceName"
          placeholder="请输入有限空间名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要危害因素" prop="mainHazardFactors">
        <el-input
          v-model="queryParams.mainHazardFactors"
          placeholder="请输入主要危害因素"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="可发生事故后果" prop="possibleAccidentConsequences">-->
<!--        <el-input-->
<!--          v-model="queryParams.possibleAccidentConsequences"-->
<!--          placeholder="请输入可发生事故后果"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="防护要求" prop="protectionRequirements">-->
<!--        <el-input-->
<!--          v-model="queryParams.protectionRequirements"-->
<!--          placeholder="请输入防护要求"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="审批责任人" prop="approvalResponsiblePerson">-->
<!--        <el-input-->
<!--          v-model="queryParams.approvalResponsiblePerson"-->
<!--          placeholder="请输入审批责任人"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="现场责任人" prop="sceneResponsiblePerson">-->
<!--        <el-input-->
<!--          v-model="queryParams.sceneResponsiblePerson"-->
<!--          placeholder="请输入现场责任人"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="remarkSpare">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用1" prop="remarkSpare1">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare1"-->
<!--          placeholder="请输入备用1"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用2" prop="remarkSpare2">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare2"-->
<!--          placeholder="请输入备用2"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ledger:space:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ledger:space:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ledger:space:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['ledger:space:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spaceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="流程/区域" align="center" prop="processRegion" />
      <el-table-column label="有限空间名称" align="center" prop="confinedSpaceName" />
      <el-table-column label="主要危害因素" align="center" prop="mainHazardFactors" :show-overflow-tooltip="true"/>
      <el-table-column label="可发生事故后果" align="center" prop="possibleAccidentConsequences" :show-overflow-tooltip="true"/>
      <el-table-column label="防护要求" align="center" prop="protectionRequirements" :show-overflow-tooltip="true"/>
      <el-table-column label="审批责任人" align="center" prop="approvalResponsiblePerson" :formatter="approvalPersonFormat"/>
      <el-table-column label="现场责任人" align="center" prop="sceneResponsiblePerson" :formatter="scenePersonFormat"/>
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="备用" align="center" prop="remarkSpare" />-->
<!--      <el-table-column label="备用1" align="center" prop="remarkSpare1" />-->
<!--      <el-table-column label="备用2" align="center" prop="remarkSpare2" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:space:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ledger:space:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改受限空间台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="流程/区域" prop="processRegion">
          <el-input v-model="form.processRegion" placeholder="请输入流程/区域" />
        </el-form-item>
        <el-form-item label="有限空间名称" prop="confinedSpaceName">
          <el-input v-model="form.confinedSpaceName" placeholder="请输入有限空间名称" />
        </el-form-item>
        <el-form-item label="主要危害因素" prop="mainHazardFactors">
          <el-input v-model="form.mainHazardFactors" type="textarea" placeholder="请输入主要危害因素" />
        </el-form-item>
        <el-form-item label="可发生事故后果" prop="possibleAccidentConsequences">
          <el-input v-model="form.possibleAccidentConsequences" type="textarea" placeholder="请输入可发生事故后果" />
        </el-form-item>
        <el-form-item label="防护要求" prop="protectionRequirements">
          <el-input v-model="form.protectionRequirements" type="textarea" placeholder="请输入防护要求" />
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="审批责任人" prop="approvalResponsiblePerson">
<!--          <el-input v-model="form.approvalResponsiblePerson" placeholder="请输入审批责任人" />-->
          <el-select  v-model="form.approvalResponsiblePerson" filterable placeholder="选择审批责任人" @change="handleChange" style="width: 100%">
            <el-option
              v-for="user in filteredUsers"
              :key="user.userId+''"
              :label="user.nickName"
              :value="user.userId+''">
            </el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="现场责任人" prop="sceneResponsiblePerson">
<!--          <el-input v-model="form.sceneResponsiblePerson" placeholder="请输入现场责任人" />-->
          <el-select  v-model="form.sceneResponsiblePerson" filterable placeholder="选择现场责任人" @change="handleChange"   style="width: 100%">
            <el-option
              v-for="user in filteredUsers"
              :key="user.userId+''"
              :label="user.nickName "
              :value="user.userId+''">
            </el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea"/>
        </el-form-item>
<!--        <el-form-item label="备用" prop="remarkSpare">-->
<!--          <el-input v-model="form.remarkSpare" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用1" prop="remarkSpare1">-->
<!--          <el-input v-model="form.remarkSpare1" placeholder="请输入备用1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用2" prop="remarkSpare2">-->
<!--          <el-input v-model="form.remarkSpare2" placeholder="请输入备用2" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpace, getSpace, delSpace, addSpace, updateSpace, exportSpace } from "@/api/ledger/space";
import { listUserNoPage } from '@/api/system/user'

export default {
  name: "Space",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 受限空间台账表格数据
      spaceList: [],
      // 弹出层标题
      title: "",
      filteredUsers:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processRegion: null,
        confinedSpaceName: null,
        mainHazardFactors: null,
        possibleAccidentConsequences: null,
        protectionRequirements: null,
        approvalResponsiblePerson: null,
        sceneResponsiblePerson: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        processRegion: [
          { required: true, message: "流程/区域不能为空", trigger: "blur" }
        ],
        confinedSpaceName: [
          { required: true, message: "有限空间名称不能为空", trigger: "blur" }
        ],
        mainHazardFactors: [
          { required: true, message: "主要危害因素不能为空", trigger: "blur" }
        ],
        possibleAccidentConsequences: [
          { required: true, message: "可发生事故后果不能为空", trigger: "blur" }
        ],
        protectionRequirements: [
          { required: true, message: "防护要求不能为空", trigger: "blur" }
        ],
        approvalResponsiblePerson: [
          { required: true, message: "审批责任人不能为空", trigger: "blur" }
        ],
        sceneResponsiblePerson: [
          { required: true, message: "现场责任人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.fetchUsers();
    this.getList();
  },
  methods: {
    /** 查询受限空间台账列表 */
    getList() {
      this.loading = true;
      listSpace(this.queryParams).then(response => {
        this.spaceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    fetchUsers() {
      this.loading=true;
      listUserNoPage().then(response => {
          this.filteredUsers = response.rows;
          this.loading = false;
        }
      );
    },
    approvalPersonFormat(row, column) {
      const user = this.filteredUsers.find(user => user.userId+'' === row.approvalResponsiblePerson);

      return user ? user.userName: '';
    },
    scenePersonFormat(row, column) {
      const user = this.filteredUsers.find(user => user.userId+'' === row.sceneResponsiblePerson);

      return user ? user.userName : '';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        processRegion: null,
        confinedSpaceName: null,
        mainHazardFactors: null,
        possibleAccidentConsequences: null,
        protectionRequirements: null,
        approvalResponsiblePerson: null,
        sceneResponsiblePerson: null,
        remark: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加受限空间台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSpace(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改受限空间台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSpace(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpace(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除受限空间台账编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSpace(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有受限空间台账数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          // return exportSpace(queryParams);
        this.download('/ledger/space/export', {
          ...this.queryParams
        }, `受限空间台账.xlsx`)
        this.exportLoading = false;
        }).then(response => {
          // this.download(response.msg);
          // this.exportLoading = false;
        }).catch(() => {});
    },
    handleChange(value) {
      console.log("Selected user ID:", value)
    },
  }
};
</script>
